package gui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.WindowConstants;

import connection.Connection;

/**
 * Okno pro konzoli
 * 
 * @author dove
 * 
 */
public class ConsoleWin extends InternalFrame {

	private static final long serialVersionUID = 1L;
	private String login;
	private String password;
	private JPanel pan;
	private JTextField in;
	private Connection con;

	/**
	 * Nastavuje nazev okna
	 * 
	 * @param name
	 *            nazev okna
	 */
	public ConsoleWin(String name) {
		super(name);
	}

	public ConsoleWin getWindow() {
		con = new Connection();
		pan = new JPanel();
		in = new JTextField();
		in.setPreferredSize(new Dimension(300, 20));
		in.setActionCommand("\n");
		ActionListener lst = new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				buttAction();
			}
		};
		in.addActionListener(lst);
		pan.setLayout(new BorderLayout());
		pan.add(new JScrollPane(con.getTextArea()), BorderLayout.CENTER);
		pan.add(in, BorderLayout.SOUTH);
		this.add(pan);
		this.pack();
		this.setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);
		return this;
	}

	/**
	 * Pripojuje konzoli
	 */
	public void connect() {
		con.connect(login, password);
	}

	/**
	 * Akce pri stisku Enter v poli
	 */
	public void buttAction() {
		con.sendString(in.getText());
		in.setText("");
	}

	/**
	 * Nastavuje prihlasovaci jmeno
	 * 
	 * @param login
	 */
	public void setLogin(String login) {
		this.login = login;
	}

	/**
	 * Nastavuje heslo
	 * 
	 * @param password
	 */
	public void setPassword(String password) {
		this.password = password;
	}

}
